﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace MvcSupplantCaching.Demo
{
    public class MvcApplication : System.Web.HttpApplication
    {
        private const string RequestTimerCacheKey = "RequestTimer";

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

        protected void Application_BeginRequest()
        {
            HttpContext.Current.Items[RequestTimerCacheKey] = Stopwatch.StartNew();
        }

        public static TimeSpan GetRequestTimerElapsed()
        {
            Stopwatch requestTimer = (Stopwatch)HttpContext.Current.Items[RequestTimerCacheKey];
            return requestTimer.Elapsed;
        }

        protected void Application_EndRequest()
        {
            HttpContext.Current.Items.Remove(RequestTimerCacheKey);
        }
    }
}
